package test.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
 
public class ThreadCommunicationTest2 {  
	
    private static char currentThread = 'A'; 
    private final Object t = new Object(); //使用一个同步对象保证两个线程之间每一时刻只有一个线程工作
      
    public static void main(String[] args) {  
        ThreadCommunicationTest2 test = new ThreadCommunicationTest2();  
        ExecutorService service = Executors.newCachedThreadPool();  
        
        service.execute(test.new RunnableA());  
        service.execute(test.new RunnableB());  
  
        service.shutdown();  
    }  
    
    private class RunnableA implements Runnable {
    	public void run() {  
        	for (int i = 1; i <= 52; i++) {  
                synchronized (t) {  
                    if(currentThread != 'A'){  
                        try {  
                            t.wait();  
                        } catch (InterruptedException e) {  
                            e.printStackTrace();  
                        }  
                    }  
                    System.out.println(i);  
                    if (i % 2 == 0) {  
                        currentThread = 'B';  
                        t.notifyAll();  
                    }  
                }  
            }  
        }
    }
    
    private class RunnableB implements Runnable {
    	public void run() {  
        	for (char c = 'A'; c <= 'Z'; c++) { 
                synchronized (t) {  
                    if(currentThread != 'B'){  
                        try {  
                            t.wait();  
                        } catch (InterruptedException e) {  
                            e.printStackTrace();  
                        }  
                    }  
                    System.out.println(c);  
                    currentThread = 'A';  
                    t.notifyAll();  
                }  
            }  
        }
    }
  
}
